<cfcomponent extends="Controller">
	<!--- [ Main ] - Login --->
	<cffunction name="login">
		<cfset User = Model("user").New()>
	</cffunction>
	
	<cffunction name="signin">
		<cfscript>
			User = Model("User").findOneByLoginAndPassword(include="UserType", value="#PARAMS.Login#,#PARAMS.Password#");

			if(isObject(User)) {
				SESSION.User.ID = User.id;
				SESSION.User.UserType = User.UserType.userType;
				SESSION.user.name = User.firstname & ' ' & User.lastName;
				
				if(User.password IS "welcome") {
					Set(Navigation = { tab = "home", tabNames = ["Password Reset"] });
					RedirectTo(controller="main", action="passwordReset");
				} else {
					Set(Navigation = {	tab = "home", tabNames = ["Home"]	});
					RedirectTo(controller="Main", action="home");
				}
			} else {
				FlashInsert(error="The <strong><em>Username</em></strong> or <strong><em>Password</em></strong> you have entered is incorrect!");
				RenderPage(controller="Main", action="login");
			}
		</cfscript>
	</cffunction>
	
	<cffunction name="passwordReset">
		<cfscript>	
			if(StructKeyExists(PARAMS, "newPassword")) {
				users = Model('user').updateByKey(key=SESSION.user.id, properties=PARAMS.newPassword);
				Set(Navigation = {	tab = "home", tabNames = ["Home"]	});
				RedirectTo(controller="Main", action="home");
			}
		</cfscript>
	</cffunction>
	
	<!--- [ Main ] - Home --->
	<cffunction name="home"></cffunction>
	
	<!--- [ Main ] - consumer Database--->
	<cffunction name="consumer"></cffunction>
	
	<!--- [ Main ] - Documents --->
	<cffunction name="document">
		<cfscript>
			Documents = Model("document").findAll(order="documentSortOrder, sortOrder");
		</cfscript>
	</cffunction>
	
	<!--- [ Main ] - Admin --->
	<cffunction name="admin">
		<cfscript>
			Admin = {};
			Admin['User Settings'] = { ID = "UserSettings", Type="User", Options=['New User', 'User Settings'] };
			Admin['Document Settings'] ={ ID = "DocumentSettings", Type="Document", Options= ['New Document','Document Settings'] };
		</cfscript>
	</cffunction>
	
	<cffunction name="adminSettings">
		<cfscript>
			user = Model("user");
			users = user.findAll(include="userType");
			users.mainPhone = REReplace(Users.mainPhone, "\(?\d{3}\)? \d{3}[- ]\d{4}", "(\1) \2-\3", "All");

			documents = {
				dosageUnits 		=	{ query = model("dosages").findAll(), 										columns = model("dosages").columnNames(), 			table="dosages"		},
				frequencyUnits 	= 	{ query = model("frequencies").findAll(), 									columns = model("frequencies").columnNames() ,	table="frequencies"	},
				dsmCodes 		=	{ query = model("axisCodes").findAll(order="category,code"), 	columns = model("axisCodes").columnNames(), 		table="axisCodes"	 	},
				racialEthnicities 	= 	{ query = model("races").findAll(), 											columns = model("races").columnNames(), 				table="races"				},
				routeMethods 	=	{ query = model("routes").findAll(),											columns = model("routes").columnNames(),				table="routes"			}
			};
		</cfscript>
	</cffunction>
	
	<!--- [ Main ] - Logout --->
	<cffunction name="logout">
		<cfscript>
			StructDelete(SESSION, 'User');
			FlashInsert(success="You have successfully logged out!");
			RenderPage(action="login");
		</cfscript>
	</cffunction>
</cfcomponent>